package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.Coin;
import com.hyk.dto.CoinDto;
import com.hyk.dto.R;
import com.hyk.feign.CoinFeign;
import com.hyk.service.CoinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/coins")
@Api(tags = "币种配置控制器")
public class CoinController implements CoinFeign {

    @Resource
    private CoinService coinService;

    @GetMapping("/all")
    @ApiOperation("all")
    public R<List<Coin>> allList(byte status){
        QueryWrapper<Coin> wrapper = new QueryWrapper<>();
        wrapper.eq("status",status);
        wrapper.orderByDesc("last_update_time");
        return R.ok(coinService.list(wrapper));
    }

    @GetMapping
    @ApiOperation("all")
    public R<Page<Coin>> list(@ApiIgnore Page<Coin> page,
                              String name,
                              String type,
                              String status,
                              String title,
                              String wallet_type){
        QueryWrapper<Coin> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(name)){
            wrapper.like("name",name);
        }
        if(StringUtils.isNotBlank(title)){
            wrapper.like("title",title);
        }
        if(StringUtils.isNotBlank(type)){
            wrapper.eq("type",type);
        }
        if(StringUtils.isNotBlank(status)){
            wrapper.eq("status",status);
        }
        if(StringUtils.isNotBlank(wallet_type)){
            wrapper.eq("wallet",wallet_type);
        }
        wrapper.orderByDesc("last_update_time");
        return R.ok(coinService.page(page,wrapper));
    }

    @PostMapping
    @ApiOperation("新增")
    public R add(@RequestBody Coin coin){
        coin.setStatus(1);
        boolean save = coinService.save(coin);
        if (save) {
            return R.ok();
        }
        return R.fail();
    }

    @PatchMapping
    @ApiOperation("修改")
    public R update(@RequestBody Coin coin){
        boolean update = coinService.updateById(coin);
        if (update){
            return R.ok();
        }
        return R.fail();
    }

    @PostMapping("/setStatus")
    @ApiOperation("禁用或启用")
    public R changeStatus(@RequestBody Coin coin){
        boolean update = coinService.updateById(coin);
        if (update){
            return R.ok();
        }
        return R.fail();
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询币种的详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "币种的id")
    })
    public R<Coin> info(@PathVariable("id") Long id){
        Coin coin = coinService.getById(id);
        return R.ok(coin) ;
    }

    @Override
    @GetMapping("/getCoinByIds")
    public Map<Long, CoinDto> getCoinByIds(List<Long> ids) {
        return coinService.getCoinByIds(ids);
    }
}
